<?php
/**
 * 用户管理功能调试工具
 * 用于测试所有用户管理功能按钮是否能正常工作
 */

// 获取当前服务器信息
$serverName = $_SERVER['SERVER_NAME'];
$serverPort = $_SERVER['SERVER_PORT'];
$currentUrl = "http://{$serverName}:{$serverPort}";

// 定义用户管理相关的所有功能URL
$userManageUrls = [
    '用户管理页面' => "http://localhost:63342/Blog/admin.php?c=User&a=user_manage",
    '新建用户' => "http://localhost:63342/Blog/admin.php?c=User&a=update",
    '编辑用户(示例ID=1)' => "http://localhost:63342/Blog/admin.php?c=User&a=update&id=1",
    '禁用用户(示例ID=1)' => "http://localhost:63342/Blog/admin.php?c=User&a=changeStatus&id=1&status=disable",
    '启用用户(示例ID=1)' => "http://localhost:63342/Blog/admin.php?c=User&a=changeStatus&id=1&status=active",
    '删除用户(示例ID=1)' => "http://localhost:63342/Blog/admin.php?c=User&a=deleteUser&id=1",
    '批量删除(示例ID=1,2)' => "http://localhost:63342/Blog/admin.php?c=User&a=batchDelete&ids=1,2",
    '搜索用户(关键词=admin)' => "http://localhost:63342/Blog/admin.php?c=User&a=search&keyword=admin"
];

// 检查URL可访问性的函数
function checkUrlAccess($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_NOBODY, true); // 只获取响应头
    
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return $httpCode != 404;
}

// 测试所有URL
$testResults = [];
foreach($userManageUrls as $name => $url) {
    $testResults[$name] = [
        'url' => $url,
        'accessible' => checkUrlAccess($url)
    ];
}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理功能调试工具</title>
    <style>
        body { font-family: 'Microsoft YaHei', Arial, sans-serif; margin: 20px; line-height: 1.6; }
        h1 { color: #333; margin-bottom: 20px; }
        .debug-container { max-width: 900px; margin: 0 auto; }
        .debug-section { margin-bottom: 30px; }
        .btn { display: inline-block; padding: 10px 20px; margin-right: 10px; margin-bottom: 10px; 
                background: #667eea; color: white; border: none; border-radius: 4px; 
                text-decoration: none; cursor: pointer; transition: all 0.3s ease; }
        .btn:hover { background: #5a6fd8; transform: translateY(-1px); }
        .btn-success { background: #28a745; }
        .btn-success:hover { background: #218838; }
        .btn-danger { background: #ff4757; }
        .btn-danger:hover { background: #ff3742; }
        .btn-warning { background: #ffbc00; }
        .btn-warning:hover { background: #e6ac00; }
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 8px;
        }
        .status-success { background-color: #28a745; }
        .status-error { background-color: #ff4757; }
        .url-list {
            background: #f8f9fa; padding: 15px; border-radius: 4px;
            margin-top: 20px;
        }
        .url-item {
            padding: 8px 0;
            border-bottom: 1px solid #e9ecef;
        }
        .url-item:last-child {
            border-bottom: none;
        }
        .url-label {
            font-weight: bold;
            color: #495057;
        }
        .url-value {
            font-family: monospace; font-size: 14px; color: #6c757d; word-break: break-all;
        }
        .explanation {
            background: #f8f9fa; padding: 20px; border-radius: 4px;
            margin-top: 30px; border-left: 4px solid #667eea;
        }
        .clear-cache-btn {
            background: #17a2b8; margin-top: 10px;
        }
        .clear-cache-btn:hover {
            background: #138496;
        }
    </style>
</head>
<body>
    <div class="debug-container">
        <h1>用户管理功能调试工具</h1>
        
        <div class="debug-section">
            <h2>功能按钮测试</h2>
            <p>点击以下按钮测试用户管理功能是否正常工作：</p>
            
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=user_manage" class="btn btn-success">访问用户管理页面</a>
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=update" class="btn btn-warning">新建用户</a>
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=update&id=1" class="btn">编辑用户(ID=1)</a>
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=changeStatus&id=1&status=disable" class="btn btn-danger">禁用用户(ID=1)</a>
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=changeStatus&id=1&status=active" class="btn btn-success">启用用户(ID=1)</a>
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=deleteUser&id=1" class="btn btn-danger">删除用户(ID=1)</a>
            <a href="http://localhost:63342/Blog/admin.php?c=User&a=search&keyword=admin" class="btn">搜索用户(admin)</a>
            <a href="http://localhost:63342/Blog/clear_all_cache.php" class="btn clear-cache-btn">清理所有缓存</a>
        </div>
        
        <div class="debug-section">
            <h2>URL配置检查</h2>
            <p>当前服务器URL配置：</p>
            <p><strong>服务器名称：</strong>{$serverName}</p>
            <p><strong>服务器端口：</strong>{$serverPort}</p>
            <p><strong>基础URL：</strong>{$currentUrl}</p>
        </div>
        
        <div class="debug-section">
            <h2>URL访问性测试</h2>
            <div class="url-list">
                <?php foreach($testResults as $name => $result): ?>
                    <div class="url-item">
                        <div class="url-label">
                            <span class="status-indicator status-<?php echo $result['accessible'] ? 'success' : 'error'; ?>"></span>
                            <?php echo $name; ?>
                        </div>
                        <div class="url-value">
                            <a href="<?php echo $result['url']; ?>" target="_blank"><?php echo $result['url']; ?></a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        
        <div class="explanation">
            <h3>问题分析与解决方案</h3>
            <p><strong>已修复的问题：</strong>user_manage.tpl模板中的"新建用户"按钮使用了相对路径(../../../admin.php)，这会导致404错误。现已修改为绝对路径。</p>
            <p><strong>验证方法：</strong>
                <ol>
                    <li>清除浏览器缓存</li>
                    <li>使用上面的按钮测试所有用户管理功能</li>
                    <li>如果仍有问题，请点击"清理所有缓存"按钮</li>
                </ol>
            </p>
            <p><strong>重要提示：</strong>所有链接现在都使用了完整的绝对URL路径，这应该能解决在不同浏览器中的404问题。如果问题仍然存在，请检查您的服务器配置或联系技术支持。</p>
        </div>
    </div>
</body>
</html>